package com.example.demo.controller;

import cn.hutool.json.JSONUtil;
import com.example.demo.config.NettyClientSington;
import com.example.demo.mapper.DeviceMapper;
import com.example.demo.pojo.RestResponse;
import com.example.demo.pojo.param.ParamReadResNew;
import com.example.demo.pojo.param.ParamSaveReqNew;
import com.example.demo.pojo.zuwang.SendCommandReq;
import com.example.demo.util.DeviceNoConnect;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/param")
@CrossOrigin
public class ParamController {

    @Autowired
    ConfigFileUtil configFileUtil;

    @PostMapping("/save")
    public RestResponse save(@RequestBody ParamSaveReqNew paramSaveReqNew) throws InterruptedException {

        RestResponse<T> tRestResponse = DeviceNoConnect.noConnect(deviceMapper, paramSaveReqNew.getTarget());
        if (!tRestResponse.isSuccessful()){
            return tRestResponse;
        }

        String message = JSONUtil.toJsonStr(paramSaveReqNew);

        //发送修改参数的指令   然后在收到接收机消息后修改param.json
        NettyClientSington.getNettyClient().sendMessage(message, paramSaveReqNew.getDevice());
        return RestResponse.success();

    }
    @Autowired
    private DeviceMapper deviceMapper;

    @PostMapping("/read")
    public RestResponse read(@RequestBody SendCommandReq readReq) throws InterruptedException {

//        if (DemoUdpNettyServerHandler.datagramPacket == null){
//            return RestResponse.validfail("接收机连接没有建立，请稍后再试");
//        }
        RestResponse<T> tRestResponse = DeviceNoConnect.noConnect(deviceMapper, readReq.getTarget());
        if (!tRestResponse.isSuccessful()){
            return tRestResponse;
        }

        String readReqMessage = JSONUtil.toJsonStr(readReq);
        NettyClientSington.getNettyClient().sendMessage(readReqMessage,readReq.getTarget());

        return RestResponse.success();

    }

    /**
     * 读取jicheng参数
     *
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/load_jicheng")
    public RestResponse load_jicheng() throws InterruptedException {

        String s = configFileUtil.readParamJson_jieshou();
        if (s != null && s.length() > 0) {

            ParamReadResNew param = JSONUtil.toBean(s, ParamReadResNew.class);
            //返回已保存的参数
            return RestResponse.success(param);
        } else {
            return RestResponse.success("暂无参数");
        }

    }

    /**
     * 读取jieshou参数
     *
     * @return
     * @throws InterruptedException
     */
    @PostMapping("/load_jieshou")
    public RestResponse load_jieshou() throws InterruptedException {

        String s = configFileUtil.readParamJson_jieshou();
        if (s != null && s.length() > 0) {

            ParamReadResNew param = JSONUtil.toBean(s, ParamReadResNew.class);
            //返回已保存的参数
            return RestResponse.success(param);
        } else {
            return RestResponse.success("暂无参数");
        }

    }
    @PostMapping("/load_remoteControl")
    public RestResponse load_remoteControl() throws InterruptedException {

        String s = configFileUtil.readParamJson_remoteControl();
        if (s != null && s.length() > 0) {

            ParamReadResNew param = JSONUtil.toBean(s, ParamReadResNew.class);
            //返回已保存的参数
            return RestResponse.success(param);
        } else {
            return RestResponse.success("暂无参数");
        }

    }


}
